using UnityEngine;
using System.Collections;

public class PlayerControl : MonoBehaviour //: StateBehaviour<TorretaControl> 
{
	public float BarraTotal;
	public float BarraDisplay;
	public GUITexture Muerte;
	public GUITexture BarraVida;
    public float LIFE = 30f;
    public float DAMAGE = 2f;
    public float ATACKSPEED = 1f;     //mientras menor sea, mas rapido
	public GameObject hudBalas;
	
	private bool muerto = false;
	private float vidaMax = 30f;
	
    void Start () 
    {       
       Muerte.enabled=false;
	   BarraTotal=BarraVida.pixelInset.width;
	   BarraDisplay=BarraTotal;
		
	}

    void OnTriggerEnter(Collider other)
    {
		if (other.name == "powerUpBala")
		{
			hudBalas.GetComponent<ControlBalas>().actualizarBalas(other.GetComponent<PowerUpBalas>().cantidad);
		}
		else if (other.name == "powerUpVida")
		{
			LIFE = LIFE + other.GetComponent<PowerUpVida>().cantidad;
			//float sumaBarra = 
		}
		
    }
	
	void OnTriggerExit(Collider other)
    {		
		
	}
  	
	public void PintaVida()
	{
		BarraVida.pixelInset = new Rect(BarraVida.pixelInset.x,BarraVida.pixelInset.y,BarraDisplay,39);
	}
	
	public void RecibirDmg(float dmg)
	{
        LIFE = LIFE - dmg;
		float restaBarra = dmg / BarraTotal;
		//BarraDisplay=BarraDisplay-(BarraTotal/15);
		BarraDisplay = BarraDisplay - restaBarra;
		
		PintaVida();
        Debug.Log("------Player_life_left: " + LIFE);
        if (LIFE <= 0)
        {
			if (! muerto){
				this.gameObject.transform.FindChild("Hero@AimStraight").FindChild("Vincent@Caminar").GetComponent<Animation>().animation.Play("Morir");
				muerto = true;
            	Invoke("Morir", 3);
			}
        }
	}

    public void Morir()
    {		
		Muerte.enabled = true;
 		Time.timeScale=0;
    }

    
	
}
